package com.loveshare.imgshow.rest;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.loveshare.common.util.HttpClientIpUtil;
import com.loveshare.common.util.HttpRequest;
import com.loveshare.imgshow.entity.Doubanmz;
import com.loveshare.imgshow.service.ImgShowService;
import com.loveshare.imgshow.util.ImgConfigUtil;
import com.loveshare.imgshow.util.ImgShowUtil;
import com.loveshare.love.entity.LoveShow;
import com.loveshare.love.service.LoveShowService;

/**
 * 
 * ClassName: ImgShowRest 
 * @Description: 异步获取图片展示控制器
 * @author 爱分享
 * @date 2017年12月20日
 */
@RestController
@RequestMapping(value="img")
public class ImgShowRest {
	@Autowired
	private ImgShowService imgShowService;
	
	@Autowired
	private LoveShowService loveShowService;
	/**
	 * 异步获取图片数据
	 * 作者:王锋
	 * 时间:2017年12月7日
	 */
	@RequestMapping(value="/imgShow_",method=RequestMethod.GET)
	public List<String> imgShowIndex(@RequestParam int page){
		List<String> imgShowList=new ArrayList<String>();
		String allHtml=ImgShowUtil.getHtml("https://www.dbmeinv.com/dbgroup/show.htm?cid=4&pager_offset="+page);
		imgShowList=ImgShowUtil.getImgStr(allHtml);
		return imgShowList;
	}
	
	/**
	 * 你懂的
	 * 作者:王锋
	 * 时间:2017年12月8日
	 */
	@RequestMapping(value="/imgShow4SW_",method=RequestMethod.GET)
	public List<String> imgShowSiWaAndPP(HttpServletRequest request,@RequestParam int page){
		List<String> imgShowList=new ArrayList<String>();
		//放置ip
		String ip=HttpClientIpUtil.getIP(request);
		ImgConfigUtil.setImgCoinfig(ip);
		//这里固定显示第一页
		String allHtml=ImgShowUtil.getHtml("https://www.dbmeinv.com/dbgroup/show.htm?cid=6&pager_offset="+page);
		String allHtmlDX=ImgShowUtil.getHtml("https://www.dbmeinv.com/dbgroup/show.htm?cid=2&pager_offset="+page);
		imgShowList=ImgShowUtil.getImgStr(allHtml+allHtmlDX);
		return imgShowList;
	}
	
	
	
	@RequestMapping(value = "/addDate",method=RequestMethod.GET)
	public String test(HttpServletRequest request){
	///////往数据库加数据
	Doubanmz doubanmz=null;
	for (int i = 0; i < 1000; i++) {
		System.out.println("当前页数："+i);
		String htmlUrl="https://www.dbmeinv.com/index.htm?cid=3&pager_offset="+i;
		String htmlStr=ImgShowUtil.getHtml(htmlUrl);
		List<String> hrefs=ImgShowUtil.getHrefHmlts(htmlStr);
		for (String s : hrefs) {
			if(s.indexOf("topic")!=-1){
				//System.out.println("======"+s);
				//拿到图片详情的大图
				String ImgUrlHtml=ImgShowUtil.getHtml(s.replace("\"", ""));
				List<String> imgs=ImgShowUtil.getImgStr(ImgUrlHtml);
				for (String img : imgs) {
					if(img.indexOf("sinaimg.cn")!=-1&&img.indexOf("https")!=-1){
						doubanmz=new Doubanmz();
						doubanmz.setMzId(UUID.randomUUID().toString());
						doubanmz.setPictureType(5);
						doubanmz.setPictureUrl(img);
						doubanmz.setTitle("test");
						imgShowService.addDoubanmz(doubanmz);
						System.out.println("数据加载中···");
					}
				}
			}
		}
	}
	/////////////////////
	return "";
	}
	
	/**
	 * 添加表白墙
	 * @return
	 */
	@RequestMapping(value="addLoveShow",method=RequestMethod.POST)
	public boolean addLoveShow(@RequestParam String nickname,
			@RequestParam String content,HttpServletRequest request){
		//特殊词过滤
		if(content.indexOf("操")!=-1||
		content.indexOf("傻逼")!=-1||
		content.equalsIgnoreCase("sql")==true||
		content.equalsIgnoreCase("delete")==true||
		content.equalsIgnoreCase("update")==true||
		content.equalsIgnoreCase("sb")==true||
		content.equalsIgnoreCase("drop")==true){
			return false;
		}
		if(nickname.length()>20){
			return false;
		}
		String ip=HttpClientIpUtil.getIP(request);
		int count=loveShowService.getLoveCount(ip);
		if(count>30){
			return false;
		}
		LoveShow loveShow=new LoveShow();
		loveShow.setContent(content);
		loveShow.setIp(ip);
		loveShow.setIpregion(HttpRequest.getIpRegion(ip));
		loveShow.setNickname(nickname);
		loveShow.setTitle(nickname);
		loveShow.setLoveId(UUID.randomUUID().toString());
		loveShowService.addLoveShow(loveShow);
		return true;
	}
}
